package com.muyi.controller;


import com.muyi.pojo.entity.Account;
import com.muyi.pojo.entity.Result;
import com.muyi.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.dynamic.datasource.annotation.DS;


import java.util.List;

@Api(tags = "管理员模块")
@CrossOrigin
@Slf4j
@RequestMapping("/account")
@RestController
public class AccountController {
    @Autowired
    private com.muyi.service.AccountService AccountService;

    /**
     * 查询管理员数据
     * @return
     */
    @DS("pnac")
    @ApiOperation(value = "查询全部管理员")
    @GetMapping
    public Result list(){
        log.info("查询全部管理员数据");
        //调用service查询管理员数据
        List<Account> AccountList =  AccountService.list();
        return Result.success(AccountList);
    }


    /**
     * 删除管理员
     * @return
     */
    @DS("pnac")
    @ApiOperation(value = "删除管理员")
    @DeleteMapping("/{account_id}")
    public Result delete(@PathVariable Integer account_id){
        log.info("根据account_id删除管理员:{}",account_id);
        //调用service删除管理员
        AccountService.delete(account_id);
        return Result.success();
    }


    /**
     * 新增管理员
     * @return
     */
    @DS("pnac")
    @ApiOperation(value = "新增管理员")
    @PostMapping
    public Result add(@RequestBody Account Account){
        log.info("新增管理员: {}" , Account);
        //调用service新增管理员
        // 密码加密
        String s1 = MD5Util.getMD5String(Account.getPassword());
        String pwdJM = MD5Util.getMD5String(s1 + MD5Util.SALT);
        Account.setPassword(pwdJM);
        AccountService.add(Account);
        return Result.success();
    }

    @ApiOperation(value = "更新管理员")
    @PutMapping
    @DS("pnac")
    public Result update(@RequestBody Account Account){
        log.info("更新管理员信息 : {}", Account);
        //调用service更新管理员
        // 密码加密
        String s1 = MD5Util.getMD5String(Account.getPassword());
        String pwdJM = MD5Util.getMD5String(s1 + MD5Util.SALT);
        Account.setPassword(pwdJM);
        AccountService.update(Account);
        return Result.success();
    }

    // 根据account_id查询管理员
    @DS("pnac")
    @ApiOperation(value = "根据管理员id查询管理员")
    @GetMapping("/{account_id}")
    public Result getByAccountId(@PathVariable Integer account_id){
        log.info("根据account_id查询管理员, account_id:{}",account_id);
        Account Account = AccountService.getByAccountId(account_id);
        return Result.success(Account);
    }

}
